package com.kuaidian.controller;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kuaidian.model.api.ResponseMsg;
import com.kuaidian.pojo.Row;
import com.kuaidian.pojo.TPhoto;
import com.kuaidian.pojo.TUser;
import com.kuaidian.service.CollectService;
import com.kuaidian.service.PhotoService;
import com.kuaidian.service.UserService;
import com.kuaidian.util.CommonUtil;
import com.kuaidian.util.JacksonMgr;
import com.kuaidian.util.constant.ErrorCode;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;

@Api(value = "Common", description = "MIS_SYS")
@Controller
@RequestMapping("/api/MIS_SYS")
public class MisController {

	@Autowired
	private UserService userService;
	@Autowired
	private CollectService collectService;
	@Autowired
	private PhotoService photoService;

	private static Logger logger = Logger.getLogger(MisController.class);

	@InitBinder
	/* Converts empty strings into null when a form is submitted */
	public void initBinder(WebDataBinder binder) {
		binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
	}

	@ApiOperation(value = "查询用户", notes = "查询用户")
	@RequestMapping(value = "queryUsers", produces = "application/json;charset=utf-8", method = RequestMethod.GET)
	public @ResponseBody Object queryUsers(@RequestParam(required=false) String phoneNo,
			                               @RequestParam Integer pageSize, 
			                               @RequestParam Integer curPage) {
		try {
			ResponseMsg retMsg = null;
			// 查询用户
			int startRow = CommonUtil.curPage2Start(pageSize, curPage);
			TUser user_q = new TUser();
			user_q.setTel(phoneNo);
			List<TUser> users = userService.queryUsers(user_q, startRow, pageSize);
			retMsg = new ResponseMsg("成功", ErrorCode.OK, users);
			String jsonString = JacksonMgr.toJSON(retMsg);
			return jsonString;
		} catch (Exception e) {
			logger.error(e.getMessage());
			return JacksonMgr.makeExceptionJSON(e);
		}
	}
	
	@ApiOperation(value = "删除用户", notes = "删除用户")
	@RequestMapping(value = "deleteUser", produces = "application/json;charset=utf-8", method = RequestMethod.GET)
	public @ResponseBody Object deleteUser(@RequestParam Integer user_id) {
		try {
			ResponseMsg retMsg = null;
			// 查询用户
			TUser user_q = new TUser();
			user_q.setId(user_id);
			int count = userService.deleteUser(user_q);
			TPhoto photo = new TPhoto();
			photo.setUser_id(user_id);
			photoService.deletePhoto(photo);
			if(count == 1){
				retMsg = new ResponseMsg("删除用户成功", ErrorCode.OK, null);
			}else{
				retMsg = new ResponseMsg("删除用户失败", ErrorCode.ERROR, null);
			}
			String jsonString = JacksonMgr.toJSON(retMsg);
			return jsonString;
		} catch (Exception e) {
			logger.error(e.getMessage());
			return JacksonMgr.makeExceptionJSON(e);
		}
	}
	
	
	@ApiOperation(value = "图片列表", notes = "图片列表")
	@RequestMapping(value = "queryPhotos", produces = "application/json;charset=utf-8", method = RequestMethod.GET)
	public @ResponseBody Object queryPhotos(@RequestParam Integer user_id,
			                               @RequestParam Integer pageSize, 
			                               @RequestParam Integer curPage) {
		try {
			ResponseMsg retMsg = null;
			// 查询用户
			int startRow = CommonUtil.curPage2Start(pageSize, curPage);
			TPhoto photo = new TPhoto();
			photo.setUser_id(user_id);
			List<TPhoto> photos = photoService.queryPhotos(photo, startRow, pageSize);
			int photo_count = photoService.countPhotos(photo);
			Row result_row = new Row();
			result_row.put("photos", photos);
			result_row.put("photo_count", photo_count);
			retMsg = new ResponseMsg("成功", ErrorCode.OK, result_row);
			String jsonString = JacksonMgr.toJSON(retMsg);
			return jsonString;
		} catch (Exception e) {
			logger.error(e.getMessage());
			return JacksonMgr.makeExceptionJSON(e);
		}
	}
	
	@ApiOperation(value = "删除图片", notes = "删除图片")
	@RequestMapping(value = "deletePhoto", produces = "application/json;charset=utf-8", method = RequestMethod.GET)
	public @ResponseBody Object deletePhoto(@RequestParam Integer photo_id) {
		try {
			ResponseMsg retMsg = null;
			// 查询用户
			TPhoto photo = new TPhoto();
			photo.setId(photo_id);
			int count = photoService.deletePhoto(photo);
			if(count == 1){
				retMsg = new ResponseMsg("删除图片成功", ErrorCode.OK, null);
			}else{
				retMsg = new ResponseMsg("删除图片失败", ErrorCode.ERROR, null);
			}
			String jsonString = JacksonMgr.toJSON(retMsg);
			return jsonString;
		} catch (Exception e) {
			logger.error(e.getMessage());
			return JacksonMgr.makeExceptionJSON(e);
		}
	}

}
